package o2o.web.actions.order.image;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.JYImageUtils;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.order.OrderInfoImageMemory;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.order.OrderInfoImage;
import o2o.dao.PO.tip.TipImageConfig;
import o2o.dao.VO.order.OrderInfoImageVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import o2o.system.data.SYS_CONFIG_PATH;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


@Controller
public class OrderImageMan {

    //获取某一订单的图片
    public static List<OrderInfoImageVO> listOfOrderNo(String orderNo) {
        if (ValidateUtil.isNull(orderNo)) {
            return null;
        }

        List<OrderInfoImage> list = OrderInfoImageMemory.getInstance().listOfOrderNo(orderNo);
        if (ValidateUtil.isAbsList(list)) {
            List<OrderInfoImageVO> voList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }

                OrderInfoImageVO vo = new OrderInfoImageVO();
                try {
                    ObjectUtils.copyPropertiesExclude(list.get(i), vo, null);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }

                if (ValidateUtil.isNotNull(vo.getImageUrl()) && ValidateUtil.isNotNetUrl(vo.getImageUrl())) {
//                    vo.setImageUrl(Common.imageUrl + vo.getImageUrl());
                    vo.setImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + vo.getImageUrl());
                }

                voList.add(vo);
            }

            return voList;
        }
        return null;
    }


    //保存图片
    public static boolean saveImage(String username, String orderNo, TipImageConfig tipImageConfig, MultipartFile file) {
        if (null == tipImageConfig) {
            return false;
        }
        if (null != file) {
            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.ORDER_IMAGE, file);
            if (null == filePath) {
                return false;
            }
            return saveImage(username, orderNo, tipImageConfig, filePath);
        }

        return true;
    }

    //保存图片
    public static boolean saveImage(String username, String orderNo, TipImageConfig tipImageConfig, String filePath) {
        if (null == tipImageConfig) {
            return false;
        }
        if (ValidateUtil.isNotNull(filePath)) {
            OrderInfoImage info = new OrderInfoImage();
            try {
                ObjectUtils.copyPropertiesExclude(tipImageConfig, info, null);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }

            long time = DateUtils.getCurrTime();

            info.setId(null);
            info.setOrderNo(orderNo);
            info.setTipImageId(tipImageConfig.getId());
            info.setUpdateUser(null);

            info.setImageUrl(filePath);
            info.setCreateTime(time);
            info.setCreateUser(username);
            info.setDelFlag(0);
            try {
                UserDaoSql.getInstance().insertObj(PojoTables.ORDER_INFO_IMAGE, info);
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
            // 增加缓存
            OrderInfoImageMemory.getInstance().addOrModifyByOrderNo(orderNo);
        }

        return true;
    }


//
//    public static boolean saveImage2(String username, String orderNo, TipImageConfig tipImageConfig, MultipartFile file) {
//        if (null == tipImageConfig) {
//            return false;
//        }
//        if (null != file) {
//            OrderInfoImage info = new OrderInfoImage();
//            try {
//                ObjectUtils.copyPropertiesExclude(tipImageConfig, info, null);
//            } catch (Exception e) {
//                e.printStackTrace();
//                return false;
//            }
//
//            long time = DateUtils.getCurrTime();
//            //存储图片
////            String filePath = JYImageUtils.saveImage(Common.ORDERIMAGE, file);
//            String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.ORDER_IMAGE, file);
//            if (null == filePath) {
//                return false;
//            }
//            info.setId(null);
//            info.setOrderNo(orderNo);
//            info.setTipImageId(tipImageConfig.getId());
//            info.setUpdateUser(null);
//
//            info.setImageUrl(filePath);
//            info.setCreateTime(time);
//            info.setCreateUser(username);
//            info.setDelFlag(0);
//            try {
//                UserDaoSql.getInstance().insertObj(PojoTables.ORDER_INFO_IMAGE, info);
//            } catch (SQLException e) {
//                e.printStackTrace();
//                return false;
//            }
//            // 增加缓存
//            OrderInfoImageMemory.getInstance().addOrModifyByOrderNo(orderNo);
//        }
//
//        return true;
//    }


}
